package com.enation.app.shop.front.api.creditrecord.service;

import com.enation.app.shop.front.api.creditrecord.ICreditRecordManager;
import com.enation.app.shop.front.api.creditrecord.entity.CreditRecord;
import com.enation.framework.database.IDaoSupport;
import com.enation.framework.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("CreditRecordService")
public class CreditRecordManager implements ICreditRecordManager {
    @Autowired
    private IDaoSupport daoSupport;
    @Override
    public int createCreditRecord(CreditRecord creditRecord) {
        this.daoSupport.insert("rbt_credit_record",creditRecord);
        int i=9;
        return 1;
    }

    @Override
    public CreditRecord getById(Integer recordid) {
        return null;
    }
    @Override
    public CreditRecord getByUserIdAndRecetTime(Integer userid) {
        String sql="select * from rbt_credit_record where record_id in" +
                "(select max(record_id) from rbt_credit_record where user_id=?)";
        return this.daoSupport.queryForObject(sql,CreditRecord.class,userid);
    }

    @Override
    public List<CreditRecord> getByUserId(Integer userid) {
        String sql="select * from rbt_credit_record where user_id=?";
        List<CreditRecord> creditRecords=daoSupport.queryForList(sql,userid);
        return creditRecords;
    }

    @Override
    public List<CreditRecord> getByUserIdAndYearMonth(Integer userid, String year, String month) {
        String sql="";
        if (StringUtil.isEmpty(month)){
             sql="select * from rbt_credit_record where user_id=?  AND  year(create_at)='"+year+"' order by create_at desc";
        }else {
             sql="select * from rbt_credit_record where user_id=?  AND  year(create_at)='"+year+"' and  month(create_at)='"+month+"' order by create_at desc";
        }
        System.out.println(sql);
        List<CreditRecord> creditRecords=daoSupport.queryForList(sql,CreditRecord.class,userid);
        return creditRecords;
    }
}
